﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace BeatEmUp.Menu
{
    public class MenuMainScreen : MenuScreen
    {
        private GameManager game;
        MouseState oldMouseState;
        int xshift = 0;
        Texture2D continueButtonImage;
        Texture2D newGameButtonImage;
        Texture2D optionsButtonImage;
        Texture2D scoresButtonImage;
        Texture2D exitButtonImage;
        Texture2D gameTitleImage;

        Rectangle continueButtonRect;
        Rectangle newGameButtonRect;
        Rectangle optionsButtonRect;
        Rectangle scoresButtonRect;
        Rectangle exitButtonRect;
        Rectangle gameTitleRect;
        private int currentX;
        private int currentY;
        double ratioX = 1, ratioY = 1;
        
        public MenuMainScreen(GameManager game) :
            base(game)
        {
            this.game = game;
            isTitleVisible = true;
            continueButtonRect = new Rectangle(((GameManager)this.game).graphics.PreferredBackBufferWidth/2-50,((GameManager)this.game).graphics.PreferredBackBufferHeight/2-160,100,50);
            newGameButtonRect = new Rectangle(((GameManager)this.game).graphics.PreferredBackBufferWidth / 2 - 50, ((GameManager)this.game).graphics.PreferredBackBufferHeight / 2 - 100, 100, 50);
            optionsButtonRect = new Rectangle(((GameManager)this.game).graphics.PreferredBackBufferWidth / 2 - 50, ((GameManager)this.game).graphics.PreferredBackBufferHeight / 2 - 40, 100, 50);
            scoresButtonRect = new Rectangle(((GameManager)this.game).graphics.PreferredBackBufferWidth / 2 - 50, ((GameManager)this.game).graphics.PreferredBackBufferHeight / 2 + 20, 100, 50);
            exitButtonRect = new Rectangle(((GameManager)this.game).graphics.PreferredBackBufferWidth / 2 - 50, ((GameManager)this.game).graphics.PreferredBackBufferHeight / 2 + 80, 100, 50);
        }

        public void LoadContent(ContentManager content)
        {
            LoadBaseContent(content);
            continueButtonImage = content.Load<Texture2D>("MenuGraphics/continueButton");
            newGameButtonImage = content.Load<Texture2D>("MenuGraphics/newGameButton");
            optionsButtonImage = content.Load<Texture2D>("MenuGraphics/optionsButton");
            scoresButtonImage = content.Load<Texture2D>("MenuGraphics/scoresButton");
            exitButtonImage = content.Load<Texture2D>("MenuGraphics/exitButton");
        }

        public void Draw(SpriteBatch sb,MenuMainComponent menu)
        {
            DrawBaseElements(sb);
            if (menu.gameWasTurnedOn==true)
                sb.Draw(continueButtonImage, continueButtonRect, Color.White);
            sb.Draw(newGameButtonImage, newGameButtonRect, Color.White);
            sb.Draw(optionsButtonImage, optionsButtonRect, Color.White);
            sb.Draw(scoresButtonImage, scoresButtonRect, Color.White);
            sb.Draw(exitButtonImage, exitButtonRect, Color.White);
        }
        public void scaleAndMoveMainMenuScreenButtons(int xShift,int resY, double ratio)
        {
            newGameButtonRect.X = continueButtonRect.X = optionsButtonRect.X = scoresButtonRect.X = exitButtonRect.X =
                       xShift;

            continueButtonRect.Y = (int)(resY / 2.0 - 160.0 * ratio);
            newGameButtonRect.Y = (int)(resY / 2.0 - 80.0 * ratio);
            optionsButtonRect.Y = resY / 2;
            scoresButtonRect.Y = (int)(resY / 2.0 + 80.0 * ratio);
            exitButtonRect.Y = (int)(resY / 2.0 + 160.0 * ratio);

            newGameButtonRect.Width = continueButtonRect.Width = optionsButtonRect.Width = scoresButtonRect.Width = exitButtonRect.Width =
                (int)(150.0 * ratio);
            newGameButtonRect.Height = continueButtonRect.Height = optionsButtonRect.Height = scoresButtonRect.Height = exitButtonRect.Height =
                (int)(78.0 * ratio);
        }

        public void Update(Microsoft.Xna.Framework.GameTime gameTime, MenuMainComponent menu)
        {
            KeyboardState newState = Keyboard.GetState();
            MouseState newMouseState = Mouse.GetState();
            currentX = game.graphics.PreferredBackBufferWidth;
            currentY = game.graphics.PreferredBackBufferHeight;
            ratioX = (double)(currentX / 1024.0);
            ratioY = (double)(currentY / 768.0);
            double ratio = 1.0;

            //buttons are 150x78 in stock
            switch (this.game.graphics.PreferredBackBufferWidth)
            {
                case 1024:
                    xshift = 512 - 75;
                    ratio = 1;
                    scaleAndMoveMainMenuScreenButtons(xshift, 768, ratio);
                    break;
                
                case 800:
                    xshift = (int)(400.0 - 75.0 * (800.0 / 1024.0));
                    ratio = (800.0 / 1024.0);
                    scaleAndMoveMainMenuScreenButtons(xshift, 600, ratio);
                    break;
                
                case 640:
                    xshift = (int)(320.0 - 75.0 * (640.0 / 1024.0));
                    ratio = (640.0 / 1024.0);
                    scaleAndMoveMainMenuScreenButtons(xshift, 480, ratio);
                    break;
            }

            if ((this.isVisibleAndActive == true) &&
                (newMouseState.LeftButton == ButtonState.Pressed) &&
                (!(oldMouseState.LeftButton == ButtonState.Pressed)))
            {
                if (this.continueButtonRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    if (menu.gameWasTurnedOn == true)
                    {
                        this.isVisibleAndActive = false;
                    }
                }

                if (this.newGameButtonRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    this.isVisibleAndActive = false;
                    menu.StartNewGame();
                }

                if (this.optionsButtonRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    this.isVisibleAndActive = false;
                    menu.optionsScreen.isVisibleAndActive = true;
                }

                if (this.scoresButtonRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    this.isVisibleAndActive = false;
                    menu.scoresScreen.isVisibleAndActive = true;
                }

                if (this.exitButtonRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    //this.isVisibleAndActive = false;
                    //menu.exitScreen.isVisibleAndActive = true;
                    this.game.Exit();
                }
                for (int i = 0; i < 100000000; i++) ;
                
            }
            oldMouseState = newMouseState;

        }       
    }
}
